package com.dzbh.cdsdriver.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dzbh.cdsdriver.R;
import com.dzbh.cdsdriver.api.ILoginService;
import com.dzbh.cdsdriver.model.ResultBean;
import com.dzbh.cdsdriver.utils.BaseResp;
import com.dzbh.cdsdriver.utils.HRetrofitNetHelper;
import com.dzbh.cdsdriver.utils.NetUtil;
import com.dzbh.cdsdriver.utils.Utils;

import java.lang.reflect.Field;

import retrofit2.Call;

import static android.content.Context.MODE_PRIVATE;

public class LogoutFragment extends BaseFragment implements View.OnClickListener ,HRetrofitNetHelper.RetrofitCallBack<ResultBean> {

    private Context mContext;
    private Button logButton;
    private EditText nameET;
    private EditText phoneET;
    private TextView titleTV;
    private SharedPreferences mSharedPreferences;
    private boolean isLogin;

    @Override
    public void onDetach() {
        super.onDetach();

        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContext = getActivity();
        View view = inflater.inflate(R.layout.fragment_logout, null);
        mSharedPreferences = mContext.getSharedPreferences(Utils.FILENAME, MODE_PRIVATE);
        isLogin = mSharedPreferences.getBoolean(Utils.LOG_FLAG, false);
        titleTV = (TextView) view.findViewById(R.id.title);
        nameET = (EditText) view.findViewById(R.id.name);
        phoneET = (EditText) view.findViewById(R.id.phoneNumber);
        logButton = (Button) view.findViewById(R.id.logout);
        logButton.setOnClickListener(this);
        updateView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    private void updateView() {
        if(!isLogin){
            phoneET.setEnabled(true);
            nameET.setEnabled(true);
            nameET.setText("");
            phoneET.setText("");
            titleTV.setText(R.string.login);
            logButton.setText(R.string.finish);
        }else{
            initValue();
        }
    }

    private void initValue(){
        phoneET.setEnabled(false);
        nameET.setEnabled(false);
        nameET.setText(mSharedPreferences.getString(Utils.NAME, ""));
        phoneET.setText(mSharedPreferences.getString(Utils.PHONE, ""));
        titleTV.setText(R.string.welcome);
        logButton.setText(R.string.logout);
    }

    private boolean checkValue(){
        if(TextUtils.isEmpty(nameET.getText().toString())){
            showToast(R.string.tip_name);
            return false;
        }
        if(TextUtils.isEmpty(phoneET.getText().toString())){
            showToast(R.string.tip_phone);
            return false;
        }
        return true;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.logout){
                if(!isLogin) {
                    if(!checkValue()){
                        return;
                    }
                    if(NetUtil.checkNetwork(mContext) == NetUtil.NO_NETWORK){
                        showToast(R.string.no_net);
                        return;
                    }
                    mDialog.setMessage(getString(R.string.tips_login));
                    mDialog.show();
                    ILoginService loginService = retrofitNetHelper.getAPIService(ILoginService.class);
                    String username = nameET.getText().toString();
                    String phone = phoneET.getText().toString();
                    final Call<BaseResp<ResultBean>> repos = loginService.userLogin(username, phone);
                    retrofitNetHelper.enqueueCall(repos, this);

                }else{
                    isLogin = false;
                    SharedPreferences.Editor editor = mSharedPreferences.edit();
                    editor.putBoolean(Utils.LOG_FLAG, false);
                    editor.putString(Utils.PHONE, "");
                    editor.putString(Utils.NAME, "");
                    editor.commit();
                    updateView();
                    showToast(R.string.logout_success);
                }
        }
    }
    @Override
    public void onSuccess(BaseResp<ResultBean> baseResp) {
        mDialog.dismiss();
        if(baseResp.getResultCode() == 200){
            showToast(R.string.success_login);
            isLogin = true;
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putBoolean(Utils.LOG_FLAG, true);
            editor.putString(Utils.PHONE, phoneET.getText().toString());
            editor.putString(Utils.NAME, nameET.getText().toString());
            editor.commit();
            initValue();
        }else{
            showToast(R.string.fail_login);
        }
    }

    @Override
    public void onFailure(String error) {
        Log.d("zgx","onFailure======"+error);
        mDialog.dismiss();
        showToast(R.string.fail_login);
    }
}
